package edu.lingnan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.lingnan.dto.result.AnswerInfo;
import edu.lingnan.entity.Answer;
import edu.lingnan.entity.Feedback;
import edu.lingnan.service.AnswerService;
import edu.lingnan.service.FeedbackService;
import edu.lingnan.mapper.FeedbackMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author kang
* @description 针对表【tb_feedback】的数据库操作Service实现
* @createDate 2022-09-16 21:09:38
*/
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback>
    implements FeedbackService{

    @Resource
    private AnswerService answerService;

    @Override
    public Page<AnswerInfo> get(Integer currentPage, Integer size, String sId) {
        Page<Feedback> feedbackPage = this.page(
                new Page<>(currentPage, size),
                new LambdaQueryWrapper<Feedback>().eq(Feedback::getSId, sId)
        );
        List<AnswerInfo> answerInfoList = feedbackPage.getRecords().stream().map(feedback -> {
            AnswerInfo answerInfo = new AnswerInfo();
            Answer answer = answerService.getOne(new LambdaQueryWrapper<Answer>().eq(Answer::getFeedbackId, feedback.getId()));
            answerInfo.setFeedback(feedback).setAnswer(answer);
            return answerInfo;
        }).collect(Collectors.toList());

        Page<AnswerInfo> answerInfoPage = new Page<>();
        answerInfoPage.setRecords(answerInfoList);
        answerInfoPage.setCurrent(feedbackPage.getCurrent());
        answerInfoPage.setSize(feedbackPage.getSize());
        answerInfoPage.setTotal(feedbackPage.getTotal());
        return answerInfoPage;
    }
}




